from django.db import models

# Create your models here.
from soft_delete_new.models import SoftDeleteModel
from django.contrib.admin import ModelAdmin

from base.models import BaseModel, LogModel


# 销售客户表
class SaleSupplier(BaseModel):
    name = models.CharField(max_length=128, verbose_name='销售客户名称')

    class Meta:
        db_table = 'sale_supplier'
        verbose_name = '销售客户表'
        verbose_name_plural = verbose_name


# 销售合同表
class SaleContract(BaseModel):
    supplier = models.ForeignKey(SaleSupplier, on_delete=models.PROTECT, verbose_name='销售客户', blank=True, null=True)
    supplier_name = models.CharField(max_length=128, verbose_name='供应商名称', blank=True, null=True)
    inventory_name = models.CharField(max_length=128, verbose_name='存货名称')
    quantity = models.DecimalField(verbose_name='数量', max_digits=15, decimal_places=2, blank=True, null=True)
    unit_price = models.DecimalField(verbose_name='单价', max_digits=15, decimal_places=2, blank=True, null=True)
    amount = models.DecimalField(verbose_name='金额', max_digits=15, decimal_places=2, blank=True, null=True)

    contract_status = models.BooleanField(verbose_name='合同是否完成', default=False)
    note = models.CharField(max_length=255, verbose_name='备注', blank=True, null=True)

    class Meta:
        db_table = 'sale_contract_contract'
        verbose_name = '销售合同表'
        verbose_name_plural = verbose_name


# 销售结算表
class SaleStatements(BaseModel):
    settlemen_amount = models.DecimalField(verbose_name='结算数量', max_digits=15, decimal_places=2, blank=True, null=True)
    settlemen_unit = models.DecimalField(verbose_name='结算单价', max_digits=15, decimal_places=2, blank=True, null=True)
    settlemen_prepayments = models.DecimalField(verbose_name='结算金额', max_digits=15, decimal_places=2, blank=True,
                                                null=True)
    contract = models.ForeignKey(SaleContract, on_delete=models.PROTECT, verbose_name='合同', blank=True, null=True)

    note = models.CharField(max_length=255, verbose_name='备注', blank=True, null=True)

    deduction = models.DecimalField(verbose_name='扣重', max_digits=15, decimal_places=2, blank=True,
                                    null=True)

    # 奖惩款
    rewards_or_punishments = models.DecimalField(verbose_name='奖惩款', max_digits=15, decimal_places=2, default=0)

    class Meta:
        db_table = 'sale_contract_statements'
        verbose_name = '销售结算表'
        verbose_name_plural = verbose_name


# 销售合同执行表               执行表对应 结算表   多对一的关系
class SaleContractDetail(BaseModel):
    contract = models.ForeignKey(SaleContract, on_delete=models.PROTECT, verbose_name='合同', blank=True, null=True)
    prepayments = models.DecimalField(verbose_name='预付款金额', max_digits=15, decimal_places=2, blank=True, null=True)
    arrived_at_now = models.DecimalField(verbose_name='今日到货量', max_digits=15, decimal_places=2, blank=True, null=True)
    # 结算表id
    statements = models.ForeignKey(SaleStatements, on_delete=models.CASCADE, verbose_name='结算表id', blank=True,
                                   null=True)
    # 结算状态
    statements_status = models.BooleanField(verbose_name='结算状态', default=False)

    note = models.CharField(max_length=255, verbose_name='备注', blank=True, null=True)

    # 用于到货未结算的 计算
    estimated_price = models.DecimalField(verbose_name='预估单价', max_digits=15, decimal_places=2, blank=True, null=True)

    # 当日到货单价  等于合同单价
    # unit_price_at_now = models.DecimalField(verbose_name='当日到货单价', max_digits=15, decimal_places=2)
    # 当日到货金额
    # arrival_quantity_at_now = models.DecimalField(verbose_name='当日到货金额', max_digits=15, decimal_places=2)

    # contract_quantity = models.DecimalField(verbose_name='合同数量')
    # pay_amount = models.DecimalField(verbose_name='付款金额')
    # arrival_quantity = models.DecimalField(verbose_name='到货量')
    #
    # settled_volume = models.DecimalField(verbose_name='已结算数量')
    # settled_volume_now = models.DecimalField(verbose_name='本次结算量')

    class Meta:
        db_table = 'sale_contract_detail'
        verbose_name = '销售合同执行表'
        verbose_name_plural = verbose_name


# 供应商表
class Supplier(BaseModel):
    name = models.CharField(max_length=128, verbose_name='供应商名称')

    class Meta:
        db_table = 'supplier'
        verbose_name = '供应商表'
        verbose_name_plural = verbose_name


# 合同表
class Contract(BaseModel):
    supplier = models.ForeignKey(Supplier, on_delete=models.PROTECT, verbose_name='供应商', blank=True, null=True)
    supplier_name = models.CharField(max_length=128, verbose_name='供应商名称', blank=True, null=True)
    inventory_name = models.CharField(max_length=128, verbose_name='存货名称')
    quantity = models.DecimalField(verbose_name='数量', max_digits=15, decimal_places=2, blank=True, null=True)
    unit_price = models.DecimalField(verbose_name='单价', max_digits=15, decimal_places=2, blank=True, null=True)
    amount = models.DecimalField(verbose_name='金额', max_digits=15, decimal_places=2, blank=True, null=True)

    contract_status = models.BooleanField(verbose_name='合同是否完成', default=False)
    note = models.CharField(max_length=255, verbose_name='备注', blank=True, null=True)

    class Meta:
        db_table = 'contract_contract'
        verbose_name = '合同表'
        verbose_name_plural = verbose_name


# 结算表
class Statements(BaseModel):
    settlemen_amount = models.DecimalField(verbose_name='结算数量', max_digits=15, decimal_places=2, blank=True, null=True)
    settlemen_unit = models.DecimalField(verbose_name='结算单价', max_digits=15, decimal_places=2, blank=True, null=True)
    settlemen_prepayments = models.DecimalField(verbose_name='结算金额', max_digits=15, decimal_places=2, blank=True,
                                                null=True)
    contract = models.ForeignKey(Contract, on_delete=models.PROTECT, verbose_name='合同', blank=True, null=True)

    note = models.CharField(max_length=255, verbose_name='备注', blank=True, null=True)

    deduction = models.DecimalField(verbose_name='扣重', max_digits=15, decimal_places=2, blank=True,
                                    null=True)

    # 奖惩款
    rewards_or_punishments = models.DecimalField(verbose_name='奖惩款', max_digits=15, decimal_places=2, default=0)


# 合同执行表               执行表对应 结算表   多对一的关系
class ContractDetail(BaseModel):
    contract = models.ForeignKey(Contract, on_delete=models.PROTECT, verbose_name='合同', blank=True, null=True)
    prepayments = models.DecimalField(verbose_name='预付款金额', max_digits=15, decimal_places=2, blank=True, null=True)
    arrived_at_now = models.DecimalField(verbose_name='今日到货量', max_digits=15, decimal_places=2, blank=True, null=True)
    # 结算表id
    statements = models.ForeignKey(Statements, on_delete=models.CASCADE, verbose_name='结算表id', blank=True, null=True)
    # 结算状态
    statements_status = models.BooleanField(verbose_name='结算状态', default=False)

    note = models.CharField(max_length=255, verbose_name='备注', blank=True, null=True)

    # 用于到货未结算的 计算
    estimated_price = models.DecimalField(verbose_name='预估单价', max_digits=15, decimal_places=2, blank=True, null=True)

    # 当日到货单价  等于合同单价
    # unit_price_at_now = models.DecimalField(verbose_name='当日到货单价', max_digits=15, decimal_places=2)
    # 当日到货金额
    # arrival_quantity_at_now = models.DecimalField(verbose_name='当日到货金额', max_digits=15, decimal_places=2)

    # contract_quantity = models.DecimalField(verbose_name='合同数量')
    # pay_amount = models.DecimalField(verbose_name='付款金额')
    # arrival_quantity = models.DecimalField(verbose_name='到货量')
    #
    # settled_volume = models.DecimalField(verbose_name='已结算数量')
    # settled_volume_now = models.DecimalField(verbose_name='本次结算量')

    class Meta:
        db_table = 'contract_detail'
        verbose_name = '合同执行表'
        verbose_name_plural = verbose_name
